package com.zt.order.web;

import com.zt.order.client.UserClient;
import com.zt.order.pojo.Order;
import com.zt.order.pojo.User;
import com.zt.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;

    @Resource
    private UserClient userClient;

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
        // 1. 根据id查询订单
        Order order =  orderService.queryOrderById(orderId);
        // 2. 查询订单用户消息,利用feign发起http请求
        User user = userClient.findById(order.getUserId());
        // 3. 包装结果
        order.setUser(user);
        return order;
    }
    @GetMapping("/query")
    public String queryOrder() {
        // 查询商品
        orderService.queryGoods();
        // 查询订单
        System.out.println("查询订单");
        return "查询订单成功";
    }
    @GetMapping("/save")
    public String saveOrder() {
        // 查询商品
        orderService.queryGoods();
        // 查询订单
        System.err.println("新增订单");
        return "新增订单成功";
    }

//   @Resource
//   private RestTemplate restTemplate;
//
//    @GetMapping("{orderId}")
//    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
//        // 1. 根据id查询订单
//        Order order =  orderService.queryOrderById(orderId);
//        // 2. 查询订单用户消息
////        String url = "http://localhost:8081/user/" + order.getUserId();
//        String url = "http://userservice/user/" + order.getUserId();
//        User user = restTemplate.getForObject(url, User.class);
//        // 3. 包装结果
//        order.setUser(user);
//        return order;
//    }
}
